package com.shd.edanpei.presenter;

import android.widget.TextView;


import com.shd.edanpei.R;
import com.shd.edanpei.base.BasePresenterImpl;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.base.data.RxBusCode;
import com.shd.edanpei.base.dialog.DialogUtil;
import com.shd.edanpei.base.network.api.ApiCreator;
import com.shd.edanpei.base.network.model.NoDataResponse;
import com.shd.edanpei.base.network.rx.BaseObserver;
import com.shd.edanpei.base.network.rx.RxTransformer;
import com.shd.edanpei.base.rxbus.RxBus;
import com.shd.edanpei.bean.ProductBean;
import com.shd.edanpei.bean.StatusBean;
import com.shd.edanpei.presenter.contract.ProductDetailsContract;
import com.shd.edanpei.util.GsonUtil;
import com.shd.edanpei.util.ToastUtil;

import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * Created by mc on 2018/5/17.
 * yang-wen-wen
 */

public class ProductDetailsPresenter extends BasePresenterImpl<ProductDetailsContract.ProductDetailsIView>
        implements ProductDetailsContract.ProductDetailsIPresenter {

    //原数据，不可改动
    private ProductBean rootBean;
    //编辑数据model，可改动
    private ProductBean editBean;

    public ProductDetailsPresenter(ProductDetailsContract.ProductDetailsIView mView) {
        super(mView);
    }

    @Override
    public void getData(final String id) {
        mBaseIView.showLoading("");
        ApiCreator.getApi().productDetils(id)
                .compose(RxTransformer.<ProductBean>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ProductBean>() {
                    @Override
                    protected void onSuccess(ProductBean data) {
                        rootBean = data;
                        editBean = GsonUtil.getGson().fromJson(GsonUtil.toJson(data), ProductBean.class);
                        mBaseIView.setData(rootBean);
                    }

                    @Override
                    public void onFinish() {
                        mBaseIView.stopLoading();
                    }
                });
    }

    @Override
    public void clickCancel() {
        editBean = GsonUtil.getGson().fromJson(GsonUtil.toJson(rootBean), ProductBean.class);
        mBaseIView.setData(rootBean);
    }

    @Override
    public void setMaxAmount(final TextView text) {
        DialogUtil.moneyLimitDialog(mBaseIView.getContext(), "订单最高额度", Constant.Unit.WANYUAN.getUnit(), new DialogUtil.Listener() {

            @Override
            public void done(String data) {
                mBaseIView.setText(text, data + Constant.Unit.WANYUAN.getUnit());
                editBean.setLoanMax(data);
            }
        });
    }

    @Override
    public void setAuditTime(String str) {
        editBean.setLoanDay(str);
    }

    @Override
    public void setRate(final TextView text) {
        DialogUtil.rateListDialog(mBaseIView.getContext(), new DialogUtil.RateListener() {
            @Override
            public void done(String str, String min, String max) {
                mBaseIView.setText(text, str);
                editBean.setRateMin(min);
                editBean.setRateMax(max);
            }
        });
    }

    @Override
    public void setAge(final TextView text) {
        DialogUtil.ageListDialog(mBaseIView.getContext(), new DialogUtil.RateListener() {
            @Override
            public void done(String str, String min, String max) {
                mBaseIView.setText(text, str);
                editBean.setAgeMin(min);
                editBean.setAgeMax(max);
            }
        });
    }

    @Override
    public void setCarDistance(final TextView text) {
        DialogUtil.moneyLimitDialog(mBaseIView.getContext(), "车公里数", Constant.Unit.WANKM.getUnit(), new DialogUtil.Listener() {

            @Override
            public void done(String str) {
                mBaseIView.setText(text, str + Constant.Unit.WANKM.getUnit());
                editBean.setTravelWkm(str);
            }
        });
    }

    @Override
    public void setCarAge(final TextView text) {
        DialogUtil.listDialog(mBaseIView.getContext(), "车年限", R.array.car_age, new DialogUtil.Listener() {
            @Override
            public void done(String data) {
                mBaseIView.setText(text, data);
                editBean.setCartYear(data);
            }
        });
    }

    @Override
    public void setHouseAge(final TextView text) {
        DialogUtil.listDialog(mBaseIView.getContext(), "住房年限", R.array.house_age, new DialogUtil.Listener() {
            @Override
            public void done(String data) {
                mBaseIView.setText(text, data);
                editBean.setRoomAge(data);
            }
        });
    }

    @Override
    public void setHouseSize(String str) {
        editBean.setRoomSize(str);
    }

    @Override
    public void setRepayWay(String str) {
        editBean.setRepayMode(str);
    }

    @Override
    public void setRepayDate(final TextView text) {
        DialogUtil.listDialog(mBaseIView.getContext(), "订单期限", R.array.repay_date, new DialogUtil.Listener() {
            @Override
            public void done(String data) {
                mBaseIView.setText(text, data);
                editBean.setRepayTerm(data);
            }
        });
    }

    @Override
    public void setNeedData(String str) {
        editBean.setLoanData(str);
    }

    @Override
    public void submit(String id, final int index) {
        editBean.setCreateTime(null);
        editBean.setUp(!editBean.isUp());
        Map<String, Object> map = checkParams();
        if (map != null && !map.isEmpty()) {
            ApiCreator.getApi().upProduct(map)
                    .compose(RxTransformer.<NoDataResponse>errorHandle())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseObserver<NoDataResponse>() {
                        @Override
                        protected void onSuccess(NoDataResponse data) {
                            ToastUtil.showToast("操作成功");
                            RxBus.get().send(RxBusCode.NOTIFY_PRODUCT_LIST, new StatusBean(index, editBean.isUp()));
                            mBaseIView.getContext().finish();
                        }
                    });
        }
    }

    private Map checkParams() {

        return GsonUtil.toMap(editBean);
    }
}
